﻿using System;
using SaleManagement.Web.App_Code;
using SaleManagement.Domains;
using SaleManagement.Services;
using DevExpress.Web.ASPxGridView;
using System.Web.UI.HtmlControls;
using System.Data;

namespace SaleManagement.Web.Pages
{
  public partial class Debt : BasePage
  {
    #region Properties
    private const string SEARCH_CUSTOMER_CODE = "Debt_Search_Customer_Code";
    private const string SEARCH_CUSTOMER_NAME = "Debt_Search_Customer_Name";
    private const string SEARCH_CUSTOMER_PHONE = "Debt_Search_Phone";
    private const string SEARCH_IS_DEBT = "Debt_Search_IsDebt";
  

    private string m_InnerError = string.Empty;
    private string m_InnerProperties = string.Empty;
    #endregion

    #region Events
    protected void Page_Load(object sender, EventArgs e)
    {
      if (!IsCallback && !IsPostBack)
      {
        #region Load Session

        txtSearchCode.Text = GetSessionSearch(SEARCH_CUSTOMER_CODE);
        txtSearchName.Text = GetSessionSearch(SEARCH_CUSTOMER_NAME);
        txtSearchPhone.Text = GetSessionSearch(SEARCH_CUSTOMER_PHONE);
        if (GetSessionSearch(SEARCH_IS_DEBT) != null)
        {
          cbxStillDebt.Checked = Convert.ToBoolean(GetSessionSearch(SEARCH_IS_DEBT));
        }

        #endregion

        LoadPaymentDetail();
      }
    }

    protected void gvPayment_CustomCallback(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewCustomCallbackEventArgs e)
    {
      string para = e.Parameters;
      if (para == "Payment")
      {
        m_InnerProperties = "Payment";
        int customerId = -1;
        int.TryParse(hfCustomerId.Value, out customerId);
        m_InnerError = DoPayment(customerId);
      }

      LoadPaymentDetail();
    }

    protected void gvPayment_CustomJSProperties(object sender, ASPxGridViewClientJSPropertiesEventArgs e)
    {
      e.Properties["cpInnerError"] = m_InnerError;
      e.Properties["cpInnerProperty"] = m_InnerProperties;
    }

    protected void gvPayment_PageIndexChanged(object sender, EventArgs e)
    {
      LoadPaymentDetail();
    }

    protected void gvPaymentDetail_CustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e)
    {
      string[] para = e.Parameters.Split('|');
      if (para[0] == "Load")
      {
        int id = -1;
        int.TryParse(para[1], out id);
        LoadPurchaseDetail(id);
      }
    }
    #endregion

    #region Methods
    private void LoadPaymentDetail()
    {
      string customerCode = null;
      string customerName = null;
      string phone = null;
      bool stillDebt = false;

      stillDebt = cbxStillDebt.Checked;
      if (!string.IsNullOrEmpty(txtSearchCode.Text.Trim()))
      {
        customerCode = txtSearchCode.Text.Trim();
      }

      if (!string.IsNullOrEmpty(txtSearchName.Text.Trim()))
      {
        customerName = txtSearchName.Text.Trim();
      }

      if (!string.IsNullOrEmpty(txtSearchPhone.Text.Trim()))
      {
        phone = txtSearchPhone.Text.Trim();
      }

      ConfigSessionSearch(SEARCH_CUSTOMER_CODE, customerCode);
      ConfigSessionSearch(SEARCH_CUSTOMER_NAME, customerName);
      ConfigSessionSearch(SEARCH_CUSTOMER_PHONE, phone);
      ConfigSessionSearch(SEARCH_IS_DEBT, stillDebt.ToString());

      DataSet ds = new CustomerService().SearchPayment(customerCode, customerName, phone,stillDebt);
      gvPayment.DataSource = ds;
      gvPayment.DataBind();
      ApplyPaymentMasterLayout();
    }

    private void ApplyPaymentMasterLayout()
    {
      gvPayment.BeginUpdate();
      try
      {
        gvPayment.ClearSort();
        gvPayment.GroupBy((DevExpress.Web.ASPxGridView.GridViewColumn)gvPayment.Columns["CustomerId"]);
      }
      catch (Exception ex)
      {
        WriteLog("Payment.aspx::Apply Master Layout", ex);
      }
      finally
      {
        gvPayment.EndUpdate();
      }
      gvPayment.ExpandAll();
    }

    private void LoadPurchaseDetail(int id)
    {
      gvPaymentDetail.DataSource = new PaymentDetailService().GetByPurchaseId(id);
      gvPaymentDetail.DataBind();
    }

    private string DoPayment(int customerId)
    {
      try
      {
        new CustomerService().DoPayment(null, customerId, Convert.ToInt32(txtMoney.Text), dePayingDate.Value as DateTime?, mmNote.Text, DateTime.Now, CurrentUser.Username);
        return string.Empty;
      }
      catch (Exception ex)
      {
        WriteLog("Payment.aspx::DoPayment", ex);
        return "Có lỗi trong quá trình lưu dữ liệu";
      }
    }
    #endregion
  }
}
